﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SocialShare.BLL;
using System.Configuration;
using Utilities;
using SocialShare.Web.Integrations;

public partial class Master_Default : System.Web.UI.MasterPage
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadUserData();
        }
    }

    private void LoadUserData()
    {
        if (!String.IsNullOrEmpty(CookieManager.LOGGED_IN_USER_ID))
        {
            StrategyProxyUser currentLoggedInUser = new StrategyProxyUser();
            currentLoggedInUser.GetCurrentUser();
            SessionManager.LOGGED_IN_USER_ID = currentLoggedInUser.User_ID;
            SessionManager.LOGGED_IN_USER_NAME = currentLoggedInUser.User_Name;
            spanUserName.InnerText = SessionManager.LOGGED_IN_USER_NAME;
            lnbLogOut.Text = "Signout";
        }
        else
        {
            lnbLogOut.Text = "Signin";
        }

        
    }

    private void RedirectToSignInPage()
    {
        ConfigReader configReader = new ConfigReader();
        String configValue = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.SiteConfigLocation.ToString()], SITE_CONFIG.MEMBERSHIP_PROVIDER_USED.ToString());
        if (configValue != "YES")
        {
            Response.Redirect("~/signin.aspx");
        }
        else
        {
            Response.Redirect("~/Account/Login.aspx");
        }
    }

    protected void lnbLogin_Click(object sender, EventArgs e)
    {    
        if (lnbLogOut.Text == "Signout")
        {
            System.Web.Security.FormsAuthentication.SignOut();
            SessionManager.LOGGED_IN_USER_ID = String.Empty;
            SessionManager.LOGGED_IN_USER_NAME = String.Empty;
            CookieManager.IsLoggedout = true;
            CookieManager.LOGGED_IN_USER_ID = String.Empty;
            CookieManager.UserName = String.Empty;
            RedirectToSignInPage();
        }
        else
        {
            RedirectToSignInPage();
        }


        /*
         
         
         HttpCookie cookie = System.Web.Security.FormsAuthentication.GetAuthCookie("Admin", true);

            System.Web.Security.FormsAuthenticationTicket ticket = System.Web.Security.FormsAuthentication.Decrypt(cookie.Value);


            System.Web.Security.FormsIdentity identity = new System.Web.Security.FormsIdentity(ticket);


            Context.User = new System.Security.Principal.GenericPrincipal(identity, null);
            Context.Response.Cookies.Add(cookie);
         
         */

    }


   

   
}
